package com.nbjtjc.safe.api.emg;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.emg.EmgWarehouse;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.service.emg.EmgWareHouseService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 应急仓库
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgWareHouse")
public class EmgWareHouseController extends AbstractController<EmgWarehouse> {
    @Autowired
    private EmgWareHouseService emgWareHouseService;

    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST})
    @SysLog("添加或者更新应急物资中的应急仓库")
    public Result addOrUpdate(EmgWarehouse emgWareHouse) {
        Result result = new Result();
        try {
            result = emgWareHouseService.addOrUpdate(emgWareHouse);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误：" + e.getMessage());
        }
        return result;
    }

    @Override
    public Result delete(int id) {
        return null;
    }

    @Override
    @RequestMapping(value = "/find", method = {RequestMethod.GET})
    public Result find(EmgWarehouse param, Page page) {
        Result result = new Result();
        try {
            result = emgWareHouseService.find(param, page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/getDeptWarehouse", method = {RequestMethod.GET})
    public Result getDeptWarehouse() {
        Result result = new Result();
        try {
            Subject subject = SecurityUtils.getSubject();
            SysUser sysUser = (SysUser) subject.getSession().getAttribute("user");
            EmgWarehouse emgWarehouse = new EmgWarehouse();
            emgWarehouse.setDeptId(sysUser.getCompanyId().intValue());
            result = emgWareHouseService.find(emgWarehouse, new Page());
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
